home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue59 / Arch / Sample / UnitObjectResultSetBase.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-05-29  |  2.5 KB  |  102 lines

  1. unit UnitObjectResultSetBase;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   UnitObjectBase, Db, DBTables;
  8.  
  9. type
  10.   TObjectResultSetBase = class;
  11.   TObjectResultSetClass = class of TObjectResultSetBase;
  12.   TObjectResultSetBase = class(TObjectBase)
  13.     Query: TQuery;
  14.   private
  15.  
  16.     FSqlWhereClause: string;
  17.     FSqlOrderByClause: string;
  18.  
  19.   protected
  20.  
  21.     function GetSqlSelectClause: string; virtual; abstract; // Provided by the sub-class
  22.     function GetSqlFromClause: string; virtual; abstract; // Provided by the sub-class
  23.     function GetProtectedSqlWhereClause: string; virtual; // Optionally provided by the sub-class
  24.  
  25.     property ProtectedSqlWhereClause: string read GetProtectedSqlWhereClause;
  26.  
  27.     function GetSelectedRecordKey: integer; virtual; abstract; // Provided by the sub-class
  28.  
  29.   public
  30.  
  31.     property SqlSelectClause: string read GetSqlSelectClause;
  32.     property SqlFromClause: string read GetSqlFromClause;
  33.     property SqlWhereClause: string read FSqlWhereClause write FSqlWhereClause;
  34.     property SqlOrderByClause: string read FSqlOrderByClause write FSqlOrderByClause;
  35.  
  36.     property SelectedKey: integer read GetSelectedRecordKey;
  37.  
  38.     procedure RefreshResultSet;
  39.  
  40.     function IsEmpty: boolean;
  41.  
  42.   end;
  43.  
  44. var
  45.   ObjectResultSetBase: TObjectResultSetBase;
  46.  
  47. implementation
  48.  
  49. {$R *.DFM}
  50.  
  51. { TObjectResultSetBase }
  52.  
  53. function TObjectResultSetBase.IsEmpty: boolean;
  54. begin
  55.   Result := (Query.Bof and Query.Eof);
  56. end;
  57.  
  58. function TObjectResultSetBase.GetProtectedSqlWhereClause: string;
  59. begin
  60.  
  61. end;
  62.  
  63. procedure TObjectResultSetBase.RefreshResultSet;
  64. var s: string;
  65. begin
  66.   Query.DisableControls;
  67.   try
  68.   Query.Close;
  69.   Query.SQL.Clear;
  70.   Query.SQL.Add('select');
  71.   Query.SQL.Add(SqlSelectClause);
  72.   Query.SQL.Add('from');
  73.   Query.SQL.Add(SqlFromClause);
  74.  
  75.   s := SqlWhereClause;
  76.   if (SqlWhereClause = '') and (ProtectedSqlWhereClause = '')
  77.     then s := ''
  78.   else if (SqlWhereClause = '') and (ProtectedSqlWhereClause <> '')
  79.     then s := ProtectedSqlWhereClause
  80.   else if (SqlWhereClause <> '') and (ProtectedSqlWhereClause = '')
  81.     then s := SqlWhereClause
  82.   else s := '(' + ProtectedSqlWhereClause + ') and (' + SqlWhereClause + ')';
  83.  
  84.   if (s <> '') then begin
  85.     Query.SQL.Add('where');
  86.     Query.SQL.Add(s);
  87.   end;
  88.  
  89.   if (SqlOrderByClause <> '') then begin
  90.     Query.SQL.Add('order by ');
  91.     Query.SQL.Add(SqlOrderByClause);
  92.   end;
  93.  
  94.   Query.Open;
  95.   finally
  96.   Query.EnableControls;
  97.   end; // try..finally
  98.   
  99. end;
  100.  
  101. end.
  102.